package com.miyuan.wm.response;

import com.miyuan.wm.enums.ExceptionCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName BaseResponse
 * @Description 返回基础信息类
 */
@Data
@ApiModel(value = "基础响应", description = "基础响应")
public class BaseResponse<T> implements Serializable {

    @ApiModelProperty(value = "返回值", notes = "返回值")
    private T data;

    @ApiModelProperty(value = "状态码", notes = "状态码")
    private Integer errCode = 200;

    @ApiModelProperty(value = "返回值描述", notes = "返回值描述")
    private String errMsg;

    @ApiModelProperty(value = "当前时间戳", notes = "当前时间戳")
    private String timeStamp;

    public BaseResponse() {
        this.errMsg = "success";
        this.timeStamp = String.valueOf(System.currentTimeMillis());
    }

    public static BaseResponse success() {
        return new BaseResponse();
    }

    public static <T> BaseResponse<T> successOf(T t) {
        BaseResponse<T> jsonResult = new BaseResponse<T>();
        jsonResult.setData(t);
        jsonResult.setTimeStamp(String.valueOf(System.currentTimeMillis()));
        return jsonResult;
    }

    public static <T> BaseResponse<T> errorOf(T t) {
        BaseResponse<T> jsonResult = new BaseResponse<T>();
        jsonResult.setData(t);
        jsonResult.setTimeStamp(String.valueOf(System.currentTimeMillis()));
        return jsonResult;
    }

    public static <T> BaseResponse<T> errorOf(ExceptionCodeEnum errorInfo) {
        BaseResponse<T> jsonResult = new BaseResponse<T>();
        jsonResult.setErrMsg(errorInfo.getMsg());
        jsonResult.setErrCode(errorInfo.getCode());
        jsonResult.setTimeStamp(String.valueOf(System.currentTimeMillis()));
        return jsonResult;
    }

    public BaseResponse(T data, Integer errCode, String errMsg) {
        this.data = data;
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.timeStamp = String.valueOf(System.currentTimeMillis());
    }

    public BaseResponse(ExceptionCodeEnum requestParamError) {
        this.errCode = requestParamError.getCode();
        this.errMsg = requestParamError.getMsg();
        this.timeStamp = String.valueOf(System.currentTimeMillis());
    }


}
